home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Macintosh Sample Code / SC.003.SillyBalls / SillyBalls.p < prev    next >
Encoding:
Text File  |  1994-11-18  |  8.6 KB  |  251 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.00                8/88
  15. #                1.01                6/92
  16. #
  17. #    Components:    SillyBalls.p        August 1, 1988
  18. #                SillyBalls.c        August 1, 1988
  19. #                PSillyBalls.make    August 1, 1988
  20. #                CSillyBalls.make    August 1, 1988
  21. #                TCSillyBalls.π        June 4, 1992
  22. #
  23. #    This is a very simple sample program that demonstrates how to use Color 
  24. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  25. #    a color window and draw randomly colored ovals in the window.
  26. #    
  27. #    The purpose is to show how to get some initial results with Color QuickDraw.
  28. #    It is a complete program and is very short to be as clear as possible.
  29. #    
  30. #    It does not have an Event Loop.  It is not fully functional in the sense that
  31. #    it does not do all the things you would expect a well behaved Macintosh 
  32. #    program to do, like size the window naturally, have an event loop, use menus, 
  33. #    etc.
  34. #
  35. #    See Sample and TESample for the general structure and MultiFinder techniques that
  36. #    we recommend that you use when building a new application.
  37. #
  38. ------------------------------------------------------------------------------}
  39.  
  40. Program SillyBalls;
  41. (*
  42.     Version 1.0: 6/2/88
  43.     
  44.     purpose        To demonstrate a simple color App using Color QuickDraw.
  45.                         It draws colored balls in a color window, then uses colored
  46.                         text inverted in the ball.  The ball location and color is Random.
  47.                         
  48.                         This program was written by Bo3b Johnson, 1/88.
  49.                         
  50.                         The inverted Bob text was a Skippy Blair special concept,
  51.                         kept for obvious aesthetic reasons.
  52.                         
  53.                         You can build the program with this junk:
  54. pascal SillyBalls.p
  55. Link SillyBalls.p.o ∂
  56.     "{MPW}Libraries:"Interface.o ∂
  57.     "{MPW}Libraries:"Runtime.o ∂
  58.     "{MPW}PLibraries:"Paslib.o ∂
  59.     -o SillyBalls
  60. SillyBalls
  61. *)
  62.  
  63. { Where does it fit:
  64.     This is a series of sample programs for those doing development
  65.     using Color QuickDraw.  Since the whole color problem depends
  66.     upon the exact effect desired, there are a number of answers
  67.     to how to use colors, from the simple to the radically complex.
  68.     These programs try to cover the gamut, so you should use 
  69.     which ever seems appropriate.  In most cases, use the simplest
  70.     one that will give the desired results.  The compatibility
  71.     rating is from 0..9 where low is better.  The more known risks 
  72.     there are the higher the rating.
  73.     
  74.     
  75.     The programs (in order of compatibility):
  76.     
  77.         SillyBalls:    (***)
  78.             This is the simplest use of Color QuickDraw, and does
  79.             not use the Palette Manager.  It draws randomly colored
  80.             balls in a color window.  This is intended to give you
  81.             the absolute minimum required to get color on the screen.
  82.             Written in straight Pascal code.
  83.             Compatibility rating = 0, no known risks.
  84.         
  85.         FracAppPalette:
  86.             This is a version of FracApp that uses only the Palette
  87.             Manager.  It does not support color table animation
  88.             since that part of the Palette Manager is not sufficient.
  89.             The program demonstrates a full color palette that is
  90.             used to display the Mandelbrot set.  It uses an offscreen
  91.             gDevice w/ Port to handle the data, using CopyBits to
  92.             draw to the window.  The Palette is automatically 
  93.             associated with each window.  The PICT files are read
  94.             and written using the bottlenecks, to save on memory
  95.             useage.
  96.             Written in MacApp Object Pascal code.
  97.             Compatibility rating = 0, no known risks.
  98.         
  99.         TubeTest:
  100.             This is a small demo program that demonstrates using the
  101.             Palette Manager for color table animation.  It uses a 
  102.             color palette with animating entries, and draws using the
  103.             Palette Manager.  There are two circles of animating colors
  104.             which gives a flowing tube effect.  This is a valid case
  105.             for using the animating colors aspect of the Palette Manager,
  106.             since the image is being drawn directly.
  107.             Written in straight Pascal code.
  108.             Compatibility rating = 0, no known risks.
  109.         
  110.         FracApp:
  111.             This is the ‘commercial quality’ version of FracApp.  This
  112.             version supports color table animation, using an offscreen
  113.             gDevice w/ Port, and handles multiple documents.  The
  114.             CopyBits updates to the screen are as fast as possible.  The
  115.             program does not use the Palette Manager, except to
  116.             provide for the system palette, or color modes with less than
  117.             255 colors.  For color table animation using an offscreen
  118.             gDevice w/ Port, it uses the Color Manager and handles the
  119.             colors itself.  Strict compatibility was relaxed to allow for
  120.             a higher performance program.  This is the most ‘real’ of the
  121.             sample programs.
  122.             Written in MacApp Object Pascal code.
  123.             Compatibility rating = 2.  (nothing will break, but it may not
  124.             always look correct.)
  125.         
  126.         FracApp300:
  127.             This doesn't support colors, but demonstrates how to create and
  128.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  129.             resolution on LaserWriters, and clipped on other printers (but
  130.             they still print).  It demonstrates how to use a high resolution
  131.             image as a PICT file, and how to print them out.
  132.             Written in MacApp Object Pascal code.
  133.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  134.             out of the ordinary, although supported.)
  135. }
  136.  
  137. USES    Types, QuickDraw, Events, Controls, Windows, TextEdit, Dialogs, Fonts, Lists,
  138.         Menus, Resources, Scrap, ToolUtils, 
  139.         OSUtils, Files, Devices, DeskBus, DiskInit, Disks, Errors, Memory, Retrace, SegLoad, Serial,
  140.         ShutDown, Slots, Sound, Start, Timer;
  141.  
  142. CONST
  143.     BallWidth    = 20;
  144.     BallHeight    = 20;
  145.     BobSize        = 8;        { Size of text in each ball. }
  146.  
  147. VAR
  148.     windRect    : Rect;
  149.     
  150.     
  151. { Initialize everything for the program, make sure we can run. }
  152.  
  153. Procedure Initialize;
  154.  
  155. VAR
  156.     mainPtr        : WindowPtr;
  157.     error        : OSErr;
  158.     theWorld    : SysEnvRec;
  159.     
  160. BEGIN
  161.     { Test the computer to be sure we can do color.  If not we would crash, which
  162.     would be bad.  If we can’t run, just beep and exit. }
  163.     error := SysEnvirons(1, theWorld);
  164.     IF NOT theWorld.hasColorQD THEN BEGIN
  165.         SysBeep (50);
  166.         ExitToShell;                        { If no color QD, we must leave. }
  167.     END;
  168.     
  169.     { Initialize all the needed managers. }
  170.     InitGraf(@qd.thePort);
  171.     InitFonts;
  172.     InitWindows;
  173.     InitMenus;
  174.     TEInit;
  175.     InitDialogs(NIL);
  176.     InitCursor;
  177.  
  178.     { To make the Random sequences truly random, we need to make the seed start
  179.     at a different number.  An easy way to do this is to put the current time
  180.     and date into the seed.  Since it is always incrementing the starting seed
  181.     will always be different.  Don’t for each call of Random, or the sequence
  182.     will no longer be random.  Only needed once, here in the init. }
  183.     GetDateTime (qd.randSeed);
  184.  
  185.     { Make a new window for drawing in, and it must be a color window.  The window is
  186.     full screen size, made smaller to make it more visible. }
  187.     windRect := qd.screenBits.bounds;
  188.     InsetRect (windRect, 50, 50);
  189.     MainPtr := NewCWindow(NIL, windRect, 'Bo3b Land', TRUE, documentProc, 
  190.                             Pointer(-1), FALSE, 0);
  191.         
  192.     SetPort(MainPtr);                        { set window to current graf port   }
  193.     TextSize(BobSize);                        { smaller font for drawing. }
  194. END;    { Initialize }
  195.  
  196.  
  197. { NewBall: make another ball in the window at a random location and color. }
  198.  
  199. Procedure NewBall;
  200.  
  201. VAR
  202.     ballColor    : RGBColor;
  203.     ballRect    : Rect;
  204.     newLeft,
  205.     newTop        : LongInt;
  206.     
  207. BEGIN
  208.     { Make a random new color for the ball. }
  209.     WITH ballColor        DO BEGIN
  210.         red := Random;   green := Random;  blue := Random;
  211.     END;
  212.     
  213.     { Set that color as the new color to use in drawing. }
  214.     RGBForeColor (ballColor);
  215.  
  216.     { Make a Random new location for the ball, that is normalized to the window size.  
  217.     This makes the Integer from Random into a number that is 0..windRect.bottom
  218.     and 0..windRect.right.  They are normalized so that we don't spend most of our
  219.     time drawing in places outside of the window. }
  220.     newTop := Random;    newLeft := Random;
  221.     newTop := ((newTop+32767) * windRect.bottom) DIV 65536;
  222.     newLeft := ((newLeft+32767) * windRect.right) DIV 65536;
  223.     SetRect(ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  224.     
  225.     { Move pen to the new location, and paint the colored ball. }
  226.     MoveTo(newLeft, newTop);
  227.     PaintOval (ballRect);
  228.     
  229.     { Move the pen to the middle of the new ball position, for the text }
  230.     MoveTo(ballRect.Left + BallWidth DIV 2 - BobSize, 
  231.             ballRect.Top + BallHeight DIV 2 + BobSize DIV 2 -1);
  232.     
  233.     { Invert the color and draw the text there.  This won’t look quite right in 1 bit
  234.     mode, since the foreground and background colors will be the same.  Color
  235.     QuickDraw special cases this to not invert the color, to avoid invisible
  236.     drawing. }
  237.     InvertColor(ballColor); 
  238.     RGBForeColor(ballColor);
  239.     DrawString('Bob');
  240. END;    { NewBall }
  241.  
  242.  
  243. BEGIN       { Main body of program SillyBalls }
  244.     Initialize;
  245.     
  246.     Repeat
  247.         NewBall;
  248.     Until Button;
  249.     
  250. END.    { SillyBalls }
  251.